#include <game/StaticResourceScreenReference.h>
#include <game/ScreenReference.h>
#include <game/Screen.h>
#include <game/ScreenDefinition.h>
#include <game/ResourceReferenceDefinition.h>
#include <game/Root.h>

#include <Gluck.h>
#include <resource/Manager.h>
#include <xml/Stream.h>

ARELLO_GAME_NAMESPACE_DECL_START

StaticResourceScreenReference::StaticResourceScreenReference(liba::filesystem::String resourcePath)
	: resourcePath(resourcePath)
{
}

StaticResourceScreenReference::~StaticResourceScreenReference()
{
}

std::auto_ptr<Screen> StaticResourceScreenReference::ConstructScreen(Root& root)
{
	ScreenDefinition definition;

	definition.SetBackendFactory(root.GetBackendFactory());
	definition.SetControlFactory(&root.GetControlFactory());

	std::auto_ptr<resource::Resource> scene_res(resource::Manager::create_resource(this->resourcePath));
	if (!xml::StreamResource(scene_res.get()).load(&xml::Root(definition.GetLoader(), "Screen")))
		THROW(GluckBadResource(__FUNCTION__, "load() failed", this->resourcePath));

	return definition.CreateScreen(root);
}

ARELLO_GAME_NAMESPACE_DECL_END